package IOStream.Exercise002;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.http.HttpUtil;

import java.util.*;

/**
 * ClassName:Dome002
 * Package:IOStream.Exercise002
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/3/5-0:09
 * @Version:v1.0
 */
public class Dome002 {
    public static void main(String[] args) {
        //利用糊涂包生成假数据,并写到文件当中
        //1.定义变量记录网址
        String familyNameNet = "https://hanyu.baidu.com/shici/detail?pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&from=kg0";
        String boyNameNet = "http://www.haoming8.cn/baobao/10881.html";
        String girlNameNet = "http://www.haoming8.cn/baobao/7641.html";
        
        //爬取数据
        String familyNameNetStr = HttpUtil.get(familyNameNet);
        String boyNameNetStr = HttpUtil.get(boyNameNet);
        String girlNameNetStr = HttpUtil.get(girlNameNet);

        //利用正则获取数据
       // ArrayList<String> familyNameTempList = getDate(familyNameNetStr, "(.{4})(，|。)", 1);
       // ArrayList<String> boyNameTempList = getDate(boyNameNetStr, "([\\u4E00-\\u9FA5]{2})(、|。)", 1);
       // ArrayList<String> girlNameTempList = getDate(girlNameNetStr, "(.. ){4}..", 0);

        List<String> familyNameTempList = ReUtil.findAll("(.{4})(，|。)", familyNameNetStr, 1);
        List<String> boyNameTempList = ReUtil.findAll("([\\u4E00-\\u9FA5]{2})(、|。)", boyNameNetStr, 1);
        List<String> girlNameTempList = ReUtil.findAll("(.. ){4}..", girlNameNetStr, 0);
        ArrayList<String> familyNameList = new ArrayList<>();
        for (String str : familyNameTempList) {
            for (int i = 0; i < str.length(); i++) {
                char c = str.charAt(i);
                familyNameList.add(c + "");
            }
        }
        //boyNameList男生的名字
        //处理方案:去重  hashSet
        ArrayList<String> boyNameList = new ArrayList<>();
        for (String str : boyNameTempList) {
            if (!boyNameList.contains(str)) {
                //如果不重在放到集合中,如果重在不要
                boyNameList.add(str);
            }
        }
        //girlNameList(女生姓名)
        //处理方案：把里面的每一个元素用空格进行切割
        ArrayList<String> girlNameList = new ArrayList<>();
        for (String str : girlNameTempList) {
            String[] arr = str.split(" ");
            for (int i = 0; i < arr.length; i++) {
                girlNameList.add(arr[i]);
            }
        }
        //生成数据
        //姓名(唯一)+性别+随机年龄
        ArrayList<String> list = getInfos(familyNameList, boyNameList, 10, girlNameList, 15);
        Collections.shuffle(list);

        //写出数据
        //细节：
        //糊涂包的相对路径,不是相对于当前项目而言的,而是相对于class文件的
        FileUtil.writeLines(list,"E:\\aaa\\name.txt","UTF-8");
    }
    /*作用：获取男生和女生的信息
     * 形参：
     *   参数一：装着姓氏的集合
     *   参数二：装着男生名字的集合
     *   参数三：装着女生名字的集合
     *   参数四：男生的个数
     *   参数五：女生的个数*/
    public static ArrayList<String> getInfos(ArrayList<String> familyNameList, ArrayList<String> boyNameList, int boyCount, ArrayList<String> girlNameList, int girlCount) {
        //1.生成男生不重复的名字
        HashSet<String> boyhs = new HashSet<>();
        while (true) {
            if (boyhs.size() == boyCount) {
                break;
            }
            //随机 Random/ Collections.shuffle()打乱集合的方式
            Collections.shuffle(familyNameList);
            Collections.shuffle(boyNameList);
            boyhs.add(familyNameList.get(0) + boyNameList.get(0));
        }
        //生成女生不重复的名字
        HashSet<String> girlhs = new HashSet<>();
        while (true) {
            if (girlhs.size() == girlCount) {
                break;
            }
            //随机 Random/ Collections.shuffle()打乱集合的方式
            Collections.shuffle(familyNameList);
            Collections.shuffle(girlNameList);
            girlhs.add(familyNameList.get(0) + girlNameList.get(0));
        }
        //名字 - 男 - 年龄
        ArrayList<String> list = new ArrayList<>();
        Random r = new Random();
        for (String boyName : boyhs) {
            //boyName表示每一个男生的名字
            int age = r.nextInt(10) + 18;
            list.add(boyName + "-男-" + age);
        }
        //生成女生的信息并添加到集合当中
        for (String girlName : girlhs) {
            //boyName表示每一个男生的名字
            int age = r.nextInt(8) + 18;//0~7  +18  18-25之间
            list.add(girlName + "-女-" + age);
        }
        return list;
    }

}
